<template>
	<view class="navbar">
		<view class="navbar-fixed" :style="{background:backgroundColor}">
			<!-- 状态栏小程序撑起高度 -->
			<view :style="{height:statusBarHeight+'px'}"></view>
			<view class="navbar-content" :style="{height:navBarHeight+'px',width:windowWidth+'px'}">
				<button class="goBack_box" hover-class="none" @click.stop="goBack">
					<u-icon name="arrow-left" :color="color" size="40" class="icon" 
						v-if="iconShow"></u-icon>
				</button>
				
				<view class="iconSlot">
					<slot></slot>
				</view>
				<view class="title" :style="{color:color}">
					{{title}}
				</view>
			</view>
		</view>
		<!-- 需要添加占位符高度  状态栏高度+导航栏高度（否则下面tab会塌陷）-->
		<!-- <view :style="{height: statusBarHeight+navBarHeight+'px'}"></view> -->
	</view>
</template>

<script>
	export default {
		name: 'navbar',
		data() {
			return {
				// statusBarHeight: 20,/* 状态栏高度 */
				// navBarHeight: 45,/* 导航栏高度 */
				// windowWidth: 375,/* 窗口宽度 */
				/* 设定状态栏默认高度 */
			};
		},
		props: {
			iconShow: {
				type: Boolean,
				default: true,
				required: true
			},
			color: {
				type: String,
				default: '#ffffff',
				required: true
			},
			title: {
				type: String,
				default: '',
				required: true
			},
			backgroundColor: {
				type: String,
				default: 'transparent',
				required: true
			},
			statusBarHeight: {
				type: Number,
				default: 375,
				required: true
			},
			statusBarHeight: {
				type: Number,
				default: 20,
				required: true
			},
			navBarHeight: {
				type: Number,
				default: 45,
				required: true
			},
		},
		computed: {},
		methods: {
			goBack() {
				const pages = getCurrentPages();
				const currentPage = pages[pages.length - 1];
				const previousPage = pages[pages.length - 2];

				// 如果没有上一页（即当前页面是第一个页面），则跳转到首页（通常是第一个页面）
				if (!previousPage) {
					uni.switchTab({
						url: '/pages/index/index' // 这里替换为你的首页路径
					});
				} else {
					// 如果有上一页，则返回上一页
					uni.navigateBack({
						delta: 1 // 返回的页面数，如果 delta 大于现有页面数，则返回到首页
					});
				}
			}
		}

	}
</script>

<style lang="scss">
	
	button {
		margin: unset;
		padding: unset;
		background-color: transparent;
		/* 消除背景色 */
		border: none;
		/* 消除边框 */
	}
	
	button:after {
		border: unset;
	}
	page {
		font-family: PingFang SC, PingFang SC-Regular;
	}

	.size {
		text-align: left;
	}

	.navbar {
		z-index: 999;

		.navbar-fixed {
			position: fixed;
			top: 0;
			left: 0;
			z-index: 99;
			width: 100%;
			opacity: 1;
			// background: transparent;

			.navbar-content {
				display: flex;
				justify-content: center;
				align-items: center;
				padding: 0 15px;
				height: 45px;
				box-sizing: border-box;
				position: relative;
				.iconSlot{
					position: absolute;
					left: 15rpx;
					top: 50%;
					transform: translateY(-50%);
				}
				.goBack_box{
					width: 100rpx;
					height: 100%;
					position: absolute;
					left: 15rpx;
					top: 50%;
					transform: translateY(-50%);
					display: flex;
					align-items: center;
				}
				.icon {
					z-index: 10;
				}

				.title {
					font-size: 34rpx;
					font-family: PingFang SC, PingFang SC-Bold;
					font-weight: 500;
					text-align: center;
					// color: #ffffff;
				}

			}

		}

	}
</style>